const common = require('../../utils/util.js')
const md5 = require('../../utils/md5.js')
const app = getApp()
Page({
    data: {
        type: null,
        target: null,
        src: '',
        width: 250, //宽度
        height: 300, //高度
        max_width: 400,
        max_height: 400,
        disable_rotate: true, //是否禁用旋转
        disable_ratio: false, //锁定比例
        limit_move: true //是否限制移动
    },
    onLoad: function(options) {
        this.setData({
            type: options.type,
            target: options.target,
            src: options.src,
            width: options.width,
            height: options.height,
            disable_ratio: options.ratio == 'true' ? true : false
        })
        this.cropper = this.selectComponent("#image-cropper");
        this.cropper.imgReset();
    },
    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },
    cropperload(e) {
        console.log('cropper加载完成');
    },
    loadimage(e) {
        wx.hideLoading();
        console.log('图片加载完成');
        this.cropper.imgReset();
    },
    clickcut(e) {
        console.log(e.detail);
        //图片预览
        wx.previewImage({
            current: e.detail.url, // 当前显示图片的http链接
            urls: [e.detail.url] // 需要预览的图片http链接列表
        })
    },
    upload() {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                wx.showLoading({
                    title: '加载中',
                })
                const tempFilePaths = res.tempFilePaths[0];
                //重置图片角度、缩放、位置
                that.cropper.imgReset();
                that.setData({
                    src: tempFilePaths
                });
            }
        })
    },
    setWidth(e) {
        this.setData({
            width: e.detail.value < 10 ? 10 : e.detail.value
        });
        this.setData({
            cut_left: this.cropper.data.cut_left
        });
    },
    setHeight(e) {
        this.setData({
            height: e.detail.value < 10 ? 10 : e.detail.value
        });
        this.setData({
            cut_top: this.cropper.data.cut_top
        });
    },
    switchChangeDisableRatio(e) {
        //设置宽度之后使剪裁框居中
        this.setData({
            disable_ratio: e.detail.value
        });
    },
    setCutTop(e) {
        this.setData({
            cut_top: e.detail.value
        });
        this.setData({
            cut_top: this.cropper.data.cut_top
        });
    },
    setCutLeft(e) {
        this.setData({
            cut_left: e.detail.value
        });
        this.setData({
            cut_left: this.cropper.data.cut_left
        });
    },
    switchChangeDisableRotate(e) {
        //开启旋转的同时不限制移动
        if (!e.detail.value) {
            this.setData({
                limit_move: false,
                disable_rotate: e.detail.value
            });
        } else {
            this.setData({
                disable_rotate: e.detail.value
            });
        }
    },
    switchChangeLimitMove(e) {
        //限制移动的同时锁定旋转
        if (e.detail.value) {
            this.setData({
                disable_rotate: true
            });
        }
        this.cropper.setLimitMove(e.detail.value);
    },
    switchChangeDisableWidth(e) {
        this.setData({
            disable_width: e.detail.value
        });
    },
    switchChangeDisableHeight(e) {
        this.setData({
            disable_height: e.detail.value
        });
    },
    submit() {
        const that = this
        this.cropper.getImg((obj) => {
            // app.globalData.imgSrc = obj.url;
            let uploadUrl
            if (this.data.type == 2) {
                uploadUrl = 'https://mozi.bailingyoupin.com/api/Upimg/up_major_img'
            } else {
                uploadUrl = 'https://mozi.bailingyoupin.com/api/Upimg/up_comment_img'
            }
            let pages = getCurrentPages(); //页面指针数组
            let prepage = pages[pages.length - 2]; //上一页面指针
            let data = {}
            let key = wx.getStorageSync('token')
            let sign = 'mdl'
            let timestamp = Date.parse(new Date())
            data.key = key
            data.timestamp = timestamp
            data = this.objKeySort(data)
            for (let i in data) {
                sign += (i + data[i])
            }
            sign += 'mdl'
            sign = md5(sign)
            data.sign = sign
            wx.showLoading({
                title: '正在上传图片',
            })
            wx.uploadFile({
                url: uploadUrl, //仅为示例，非真实的接口地址
                filePath: obj.url,
                name: 'file',
                formData: {
                    key: data.key,
                    timestamp: data.timestamp,
                    sign: data.sign
                },
                success(res) {
                    wx.hideLoading()
                    if (JSON.parse(res.data).code == 204) {
                        wx.showModal({
                            title: '提示',
                            content: JSON.parse(res.data).error,
                            showCancel: false
                        })
                        return
                    }
                    if (that.data.type == 3) {
                        wx.showLoading({
                            title: '请稍后'
                        })
                        let data = {
                            heard_img: JSON.parse(res.data).datas
                        }
                        common.post('User/up_info', data).then(function(res) {
                            wx.hideLoading()
                            if (res.code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: res.error,
                                    showCancel: false
                                })
                                return
                            }
                            wx.navigateBack({
                                delta: -1
                            })
                        }).catch(function(e) {})
                    } else {
                        prepage.setData({
                            [that.data.target]: JSON.parse(res.data).datas
                        })
                        wx.navigateBack({
                            delta: -1
                        })
                    }
                }
            })
        });
    },
    rotate() {
        //在用户旋转的基础上旋转90°
        this.cropper.setAngle(this.cropper.data.angle += 90);
    },
    top() {
        this.data.top = setInterval(() => {
            this.cropper.setTransform({
                y: -3
            });
        }, 1000 / 60)
    },
    bottom() {
        this.data.bottom = setInterval(() => {
            this.cropper.setTransform({
                y: 3
            });
        }, 1000 / 60)
    },
    left() {
        this.data.left = setInterval(() => {
            this.cropper.setTransform({
                x: -3
            });
        }, 1000 / 60)
    },
    right() {
        this.data.right = setInterval(() => {
            this.cropper.setTransform({
                x: 3
            });
        }, 1000 / 60)
    },
    narrow() {
        this.data.narrow = setInterval(() => {
            this.cropper.setTransform({
                scale: -0.02
            });
        }, 1000 / 60)
    },
    enlarge() {
        this.data.enlarge = setInterval(() => {
            this.cropper.setTransform({
                scale: 0.02
            });
        }, 1000 / 60)
    },
    end(e) {
        clearInterval(this.data[e.currentTarget.dataset.type]);
    }
})